import { Controller, Post, Get, Delete, Body, Query, Param } from '@nestjs/common';
import { WordService } from './word.service';
import { SegmentTextDto } from './dto/segment-text.dto';
import { AddWordRelationDto } from './dto/add-word-relation.dto';
import { SaveTextRecordDto } from './dto/save-text-record.dto';

@Controller('word')
export class WordController {
  constructor(private readonly wordService: WordService) {}

  /**
   * 分词接口
   */
  @Post('segment')
  async segmentText(@Body() dto: SegmentTextDto) {
    return this.wordService.segmentText(dto);
  }

  /**
   * 搜索可替换的词汇
   */
  @Get('replaceable/:word')
  async searchReplaceableWords(@Param('word') word: string) {
    return this.wordService.searchReplaceableWords(word);
  }

  /**
   * 添加词汇关系
   */
  @Post('relation')
  async addWordRelation(@Body() dto: AddWordRelationDto) {
    return this.wordService.addWordRelation(dto);
  }

  /**
   * 删除词汇关系
   */
  @Delete('relation')
  async removeWordRelation(
    @Query('sourceWord') sourceWord: string,
    @Query('targetWord') targetWord: string,
  ) {
    return this.wordService.removeWordRelation(sourceWord, targetWord);
  }

  /**
   * 保存文本记录
   */
  @Post('record')
  async saveTextRecord(@Body() dto: SaveTextRecordDto) {
    return this.wordService.saveTextRecord(dto);
  }

  /**
   * 获取文本记录历史
   */
  @Get('records')
  async getTextRecords(
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    const pageNum = page ? parseInt(page, 10) : 1;
    const limitNum = limit ? parseInt(limit, 10) : 10;
    return this.wordService.getTextRecords(pageNum, limitNum);
  }
}